package com.example.mobile2021606163crw0408;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private CheckBox chkboxMainYinqing;
    private CheckBox chkboxMainBoli;
    private CheckBox chkboxMainFangxiangpang;
    private Button btnMainBuy;
    private TextView tvMainGouwuche;
    private TextView tvMainResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //复选框选中状态变化事件
        chkboxMainYinqing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //根据空间是否选中的状态，在文本框显示对应的信息
                if (isChecked){
                    tvMainResult.setText("已选中:"+buttonView.getText().toString());
                }else {
                    tvMainResult.setText("取消选中："+buttonView.getText().toString() );
                }
            }
        });
        chkboxMainBoli.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //根据空间是否选中的状态，在文本框显示对应的信息
                if (isChecked){
                    tvMainResult.setText("已选中:"+buttonView.getText().toString());
                }else {
                    tvMainResult.setText("取消选中："+buttonView.getText().toString() );
                }
            }
        });
        chkboxMainFangxiangpang.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //根据空间是否选中的状态，在文本框显示对应的信息
                if (isChecked){
                    tvMainResult.setText("已选中:"+buttonView.getText().toString());
                }else {
                    tvMainResult.setText("取消选中："+buttonView.getText().toString() );
                }
            }
        });
        //添加按钮点击事件的监听器
        btnMainBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //根据空间是否选中的状态，在文本框显示对应的信息
                String msg = "选中";
                //依次判断复选框选中状态
                if (chkboxMainYinqing.isChecked()){
                    Toast.makeText(MainActivity.this,"购买成功",Toast.LENGTH_SHORT).show();
                    msg = "\n"+chkboxMainYinqing.getText().toString();
                    new AlertDialog.Builder(MainActivity.this)
                            .setTitle("官方购买信息")
                            .setMessage("再逛逛，看看其他产品吧嘻嘻嘻！")
                            .setPositiveButton("再逛逛",null)
                            .setNegativeButton("不了谢谢",null)
                            .create()
                            .show();
                }
                if (chkboxMainBoli.isChecked()){
                    Toast.makeText(MainActivity.this,"购买成功",Toast.LENGTH_SHORT).show();
                    msg = "\n"+chkboxMainBoli.getText().toString();
                    new AlertDialog.Builder(MainActivity.this)
                            .setTitle("官方购买信息")
                            .setMessage("再逛逛，看看其他产品吧嘻嘻嘻！")
                            .setPositiveButton("再逛逛",null)
                            .setNegativeButton("不了谢谢",null)
                            .create()
                            .show();
                }
                if (chkboxMainFangxiangpang.isChecked()) {
                    Toast.makeText(MainActivity.this,"购买成功",Toast.LENGTH_SHORT).show();
                    msg = "\n" + chkboxMainFangxiangpang.getText().toString();
                    new AlertDialog.Builder(MainActivity.this)
                            .setTitle("官方购买信息")
                            .setMessage("再逛逛，看看其他产品吧嘻嘻嘻！")
                            .setPositiveButton("再逛逛",null)
                            .setNegativeButton("不了谢谢",null)
                            .create()
                            .show();
                }
                tvMainResult.setText(msg);
            }
        });
    }
//通过ID获取控件
    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
        chkboxMainYinqing = (CheckBox) findViewById(R.id.chkbox_main_yinqing);
        chkboxMainBoli = (CheckBox) findViewById(R.id.chkbox_main_boli);
        chkboxMainFangxiangpang = (CheckBox) findViewById(R.id.chkbox_main_fangxiangpang);
        btnMainBuy = (Button) findViewById(R.id.btn_main_buy);
        tvMainGouwuche = (TextView) findViewById(R.id.tv_main_gouwuche);
        tvMainResult = (TextView) findViewById(R.id.tv_main_Result);
    }
}